# Check for different levels of rebellion pre/post Blair leader and Blair/Brown transition
# Borrows from script for merging raw data for analysis
# Merges in and then drops Free Votes
# Drops party switchers
# Drops free votes from speech files

# Check pre Blair Leader / post Blair leader early period

rm(list=ls())
library(plyr)

# Set path
path <- "~/Dropbox (Personal)/Rebel Summaries/APSR_SKLLO_RepFiles"

setwd(path)

#### Start by reading in party affilation and leader data containing full set of MPs and positions
#### Even though we don't use the leader info here, I merge it to make sure we are working with the same set of MPs as in the full analysis
#### E.g. get rid of party switchers.

leader <- read.csv(paste(path,"/RawData/UK HoC Leadership Data 1979-2016.csv",sep=""))

## Keep data only for the periods we are looking at
leader9297 <- leader[,grep("X1992_4",colnames(leader),perl=T):grep("X1997_4",colnames(leader),perl=T)]

leader0510 <- leader[,grep("X2005_5",colnames(leader),perl=T):grep("X2010_4",colnames(leader),perl=T)]

### Get rid members who were not in parl or only served a partial term
### 0 in these data mean MP was not in parl at the time. 
### Keep PubWhipIDs only of those MPs serving a full term in each parl.
### Drop partyswitchers within term by keeping only MPs labelled Lab and Con throughout

leader9297.mp <- leader9297>0
leader9297    <- leader9297[rowMeans(leader9297.mp)==1 & (leader$Party.Affiliation==1 | leader$Party.Affiliation==2 ) , ]
PubWhipID.92  <- leader$PubWhipID[rowMeans(leader9297.mp)==1 & (leader$Party.Affiliation==1 | leader$Party.Affiliation==2 )]

leader0510.mp <- leader0510>0
leader0510 <- leader0510[rowMeans(leader0510.mp)==1  & (leader$Party.Affiliation==1 | leader$Party.Affiliation==2 ) ,]
PubWhipID.05 <- leader$PubWhipID[rowMeans(leader0510.mp)==1 & (leader$Party.Affiliation==1 | leader$Party.Affiliation==2 ) ]

### Create dummy for members who were leaders throughout the whole parl.
### Leaders are 6 and above (Min of State/AG/ and opp equivalents and above)

leader9297.bin.ld <- leader9297>=6
leader9297.ld <- rep(0,length(PubWhipID.92))
leader9297.ld[rowMeans(leader9297.bin.ld)==1] <- 1
bin.leader9297 <- as.data.frame(cbind(PubWhipID.92,leader9297.ld))

colnames(bin.leader9297) <- c("PubWhipID","leader.all")
bin.leader9297 <- bin.leader9297[!is.na(bin.leader9297$PubWhipID),]


leader0510.bin.ld <- leader0510>=6
leader0510.ld <- rep(0,length(PubWhipID.05))
leader0510.ld[rowMeans(leader0510.bin.ld)==1] <- 1
bin.leader0510 <- as.data.frame(cbind(PubWhipID.05,leader0510.ld))

colnames(bin.leader0510) <- c("PubWhipID","leader.all")
bin.leader0510 <- bin.leader0510[!is.na(bin.leader0510$PubWhipID),]



### Now read in rebel data
### Early period 1992--2001

## Read in data for 92-97
dta.92 <- read.csv(paste(path,"/RawData/1992-1993/rebels.csv",sep=""))
dta.93 <- read.csv(paste(path,"/RawData/1993-1994/rebels.csv",sep=""))
dta.94 <- read.csv(paste(path,"/RawData/1994-1995/rebels.csv",sep=""))
dta.95 <- read.csv(paste(path,"/RawData/1995-1996/rebels.csv",sep=""))
dta.96 <- read.csv(paste(path,"/RawData/1996-1997/rebels.csv",sep=""))

## merge rebel data for 92-97 
dta.temp <- join(dta.92,dta.93,by="PubWhipID",type="full")
dta.temp <- join(dta.temp,dta.94,by="PubWhipID",type="full")
dta.temp <- join(dta.temp,dta.95,by="PubWhipID",type="full")
dta.9297 <- join(dta.temp,dta.96,by="PubWhipID",type="full")

totdiv.9297 <- ncol(dta.9297)-5

# Read in free vote data for 92-97
fv.92 <- read.csv(paste(path,"/RawData/1992-1993/divisions.csv",sep=""))
fv.93 <- read.csv(paste(path,"/RawData/1993-1994/divisions.csv",sep=""))
fv.94 <- read.csv(paste(path,"/RawData/1994-1995/divisions.csv",sep=""))
fv.95 <- read.csv(paste(path,"/RawData/1995-1996/divisions.csv",sep=""))
fv.96 <- read.csv(paste(path,"/RawData/1996-1997/divisions.csv",sep=""))

fv.9297 <- rbind(fv.92,fv.93,fv.94,fv.95,fv.96)

# Subset and then drop free votes 
fdiv.9297 <- fv.9297$Division[fv.9297$FreeVote==1]

fdiv.9297 <- paste("X", as.character(fdiv.9297), sep = "")
fdiv.9297 <- gsub("-",".",fdiv.9297)

# Drop free votes (if desired... comment out to keep them)
dta.9297 <- dta.9297[,colnames(dta.9297) %in% fdiv.9297==F]


## Read in later period data
dta.0510 <- read.csv(paste(path,"/RawData/2005-2010/rebels.csv",sep=""))
totdiv.0510 <- ncol(dta.0510)-5

## Read in later period free vote data
fv.0510 <- read.csv(paste(path,"/RawData/2005-2010/divisions.csv", sep=""))

fdiv.0510 <- fv.0510$Division[fv.0510$FreeVote==1|fv.0510$FreeVLab==1|fv.0510$FreeVCon==1]

fdiv.0510 <- paste("X", as.character(fdiv.0510), sep = "")
fdiv.0510 <- gsub("-",".",fdiv.0510)

dta.0510 <- dta.0510[,colnames(dta.0510) %in% fdiv.0510==F]


#### Merge rebel vote data with leader data 
#### This allows us to drop other parties and partyswitchers, too
#### Move leader.all variable up front (drop duplicate PubWhipID var)

dta.9297 <- join(dta.9297, bin.leader9297,by="PubWhipID", type="inner") 

dta.9297 <- dta.9297[,c(1:5,ncol(dta.9297),7:ncol(dta.9297)-1)]

dta.0510 <- join(dta.0510, bin.leader0510,by="PubWhipID", type="inner") 

dta.0510 <- dta.0510[,c(1:5,ncol(dta.0510),7:ncol(dta.0510)-1)]


#### Divide into pre-Blair / Post-Blair

preblair <- dta.9297[dta.9297$Party=="Lab",c(1:691)]

postblair <- dta.9297[dta.9297$Party=="Lab",c(1:6,692:ncol(dta.9297))]

prebrown <- dta.0510[dta.0510$Party=="Lab",c(1:492)]

postbrown <- dta.0510[dta.0510$Party=="Lab",c(1:6,493:ncol(dta.0510))]

### Calculate total number of non-free votes

totdiv.pre.9297 <- ncol(preblair)-6
totdiv.post.9297 <- ncol(postblair)-6

totdiv.pre.0510 <- ncol(prebrown)-6
totdiv.post.0510 <- ncol(postbrown)-6

###

tot.reb.pre.bl <- rowSums(preblair[,7:ncol(preblair)],na.rm=T)/totdiv.pre.9297
tot.reb.post.bl <- rowSums(postblair[,7:ncol(postblair)],na.rm=T)/totdiv.post.9297

means.blair <- t.test(tot.reb.pre.bl,tot.reb.post.bl)
means.blair

tot.reb.pre.br <- rowSums(prebrown[,7:ncol(prebrown)],na.rm=T)/totdiv.pre.0510
tot.reb.post.br <- rowSums(postbrown[,7:ncol(postbrown)],na.rm=T)/totdiv.post.0510

means.brown <- t.test(tot.reb.pre.br,tot.reb.post.br)
means.brown

## There is no statistical difference in the level of Labour Party defections pre/post Blair or pre/post Brown 
